package com.practice.service.admin.impl;


import com.practice.common.result.Result;
import com.practice.mapper.admin.AdminMenuMapper;
import com.practice.pojo.admin.dto.AdminMenuDTO;
import com.practice.pojo.admin.entity.Menu;
import com.practice.pojo.admin.vo.AdminChildrenVO;
import com.practice.service.admin.AdminMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminMenuServiceImpl implements AdminMenuService {

    @Autowired
    private AdminMenuMapper adminMenuMapper;

    @Override
    public List<AdminChildrenVO> showAllDate(AdminChildrenVO adminChildrenVO) {

        List<AdminChildrenVO> adminChildrenVOList = adminMenuMapper.getByPid(adminChildrenVO);
        //非空校验
        if (adminChildrenVO == null){
            return null;
        }

        for (AdminChildrenVO childrenVO : adminChildrenVOList) {
            childrenVO.setChildren(showAllDate(childrenVO));
        }

        return adminChildrenVOList;
    }

    @Override
    public AdminChildrenVO rowInfo(Integer id) {
        AdminChildrenVO adminChildrenVO = adminMenuMapper.queryById(id);
        return adminChildrenVO;
    }

    @Override
    public Result add(AdminMenuDTO adminMenuDTO) {
        adminMenuDTO.setCreate_date(LocalDateTime.now().toString());
        adminMenuDTO.setUpdate_date(LocalDateTime.now().toString());
        adminMenuMapper.add(adminMenuDTO);
        return Result.success();
    }

    @Override
    public Result update(AdminMenuDTO adminMenuDTO) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(adminMenuDTO,menu);
        menu.setUpdateDate(LocalDateTime.now());
        adminMenuMapper.update(menu);
        return Result.success();
    }

    @Override
    public Result delete(Integer id) {
        adminMenuMapper.deleteById(id);
        return Result.success();
    }
}
